$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'projectinfo/projectinfos/auditlist',
        datatype: "json",
        colModel: [
            { label: '项目ID', name: 'projectinfoid', index: 'ProjectInfoId', width: 60, key: true },
            { label: '发布者', name: 'useraccount', index: 'CreateUser', width: 80 },
            { label: '项目名称', name: 'projectname', index: 'ProjectName', width: 80 },
            { label: '地区', name: 'areaname', index: 'AreaInfoId', width: 65 },
            { label: '项目类型', name: 'dictionaryvalueinzhcn', index: 'ProjectType', width: 80 ,formatter: function(value, options, row){
                    return value == 0 ? "工程" : "车队"}},
            { label: '项目人数', name: 'peoplenum', index: 'PeopleNum', width: 80 },
            { label: '更新时间', name: 'updatetime', index: 'UpdateTime', width: 80 },
            { label: '审核状态', name: 'auditstatus', index: 'AuditStatus', width: 70 ,formatter: function(value, options, row){
                    switch(value){
                        case 0:
                            return '<span class="label label-warning">待审核</span>';
                        case 1:
                            return '<span class="label label-success">审核通过</span>';
                        case 2:
                            return '<span class="label label-danger">审核失败</span>';
                    }
                }}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        postData:{"projectname":vm.projectname},
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
//获取token
var token = localStorage.getItem("token");
//富文本框初始化
var E = window.wangEditor;
var editor = new E('#editor');
$.base64.utf8encode = true;
var vm = new Vue({
    el:'#rrapp',
    data:{
        //选择用户界面用户查询参数
        selectUserSearchParam:null,
        //保存选择界面的名称
        userinfo:{useraccount:''},
        //判断当前按钮执行的什么操作的标识（新增，修改，等）
        btn:null,
        showList: true,
        title: null,
        //项目信息
        projectinfos: {auditstatus:null},
        //项目类型数组
        projecttypes:[],
        //数据字典项目类型查询关键字标识
        urlProjectTypeName:"projectType",
        //项目查询参数
        searchParam:'',
        //用户界面保存用户信息
        users:{},
        usershow:true,
        //用户查询的参数
        username:'',
        //每页条数
        userlimit:10,
        //排序
        order:"asc"
    },
    mounted: function(){
        console.log(editor);
        //初始化富文本编辑框
        editor.customConfig.uploadImgShowBase64 = true
        // editor.customConfig.uploadImgServer = '/upload';
        // editor.customConfig.uploadFileName = 'myFileName';
        editor.customConfig.uploadImgHeaders = {
            'token': token
        };
        editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024;
        editor.customConfig.uploadImgMaxLength = 5;
        editor.customConfig.onchange = (html) => {
            //将数据编码
            var result = htmlEncodeByRegExp(html);
            this.projectinfos.description = result;
        };
        editor.create();
    },
    methods: {
        query: function () {
            vm.reload();
        },
        audit: function (event) {
            var projectinfoid = getSelectedRow();
            if (projectinfoid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "审核";
            vm.getInfo(projectinfoid)
        },
        saveOrUpdate: function (event) {
            var url = "projectinfo/projectinfos/audit";
            if (vm.projectinfos.auditstatus == 1) {//选择通过，并且填写了原因
                vm.projectinfos.cannotpassreason = null;
            }
            var projectinfo = {projectinfoid:vm.projectinfos.projectinfoid,auditstatus:vm.projectinfos.auditstatus,cannotpassreason:vm.projectinfos.cannotpassreason};
            $.ajax({
                beforeSend: function (XMLHttpRequest) {
                    XMLHttpRequest.setRequestHeader("token", token);
                },
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(projectinfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                            //清空选项中的数据
                            vm.projectinfos = {};
                            vm.userinfo={useraccount:''};
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getInfo: function (projectinfoid) {
            $.get(baseURL + "projectinfo/projectinfos/auditinfo/" + projectinfoid, function (r) {
                var data = r.projectinfos;
                vm.projectinfos = data;
                vm.userinfo.useraccount = vm.projectinfos.useraccount;
                //设置富文本内容
                if (data.description) {
                    //传输json数据格式问题，解码
                    data.description = $.base64.atob(data.description, true);
                    editor.txt.html(data.description);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {"searchParam": vm.searchParam},
                page: page
            }).trigger("reloadGrid");
            vm.projectinfos = {};
            vm.userinfo = {};
            editor.txt.clear();
        },
        reason:function(){
            if(this.projectinfos == null){
                return false;
            }
            //审核状态当前为不同就显示
            if(this.projectinfos.auditstatus == 2){
                return true;
            }
            //审核状态为待审核，且有原因就显示
            if(this.projectinfos.auditstatus == 0 && this.projectinfos.cannotpassreason != null && this.projectinfos.cannotpassreason != ""){
                return true;
            }
            return false;
        }
    }
});